﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MvcMall.Core.Commands;
using MvcMall.Core.Contacts.Tasks;
using MvcMall.Core.Enums;
using MvcMall.Framework.Extensions;
using MvcMall.Framework.UnitOfWork;

namespace MvcMall.AdminWeb.Manage.Mails
{
    public partial class MailTemplateSave : ManageBasePage
    {
        private IMailTemplateTask _mailTemplateTask;

        protected void Page_Load(object sender, EventArgs e)
        {
            ValidatePagePower("MailTemplateIndex");
            _mailTemplateTask = GetInstance<IMailTemplateTask>();

            if (!IsPostBack)
            {
                ddlMailTemplateTypies.DataSource = MailTemplateType.注册激活通知.ToSelectList();
                ddlMailTemplateTypies.DataBind();

                var id = 0;
                if (Request.QueryString["Id"] != null && Int32.TryParse(Request.QueryString["Id"], out id))
                {
                    var info = _mailTemplateTask.Get(id);
                    if (info == null)
                    {
                        ShowMsgAndReturn("参数错误");
                        return;
                    }

                    lblId.Text = info.Id.ToString();
                    txtTitle.Text = info.Title;
                    txTemplateName.Text = info.TemplateName;
                    txtMailBody.Text = info.MailBody;
                    ddlMailTemplateTypies.SelectedIndex =
                        ddlMailTemplateTypies.Items.IndexOf(ddlMailTemplateTypies.Items.FindByValue(info.MailTemplateType.ToString()));
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var title = txtTitle.Text.Trim();
            var mailBody = txtMailBody.Text;
            var templateName = txTemplateName.Text.Trim();

            if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(mailBody) || string.IsNullOrEmpty(templateName))
            {
                ShowMsg("输入不完整");
                return;
            }

            var cmd = new SaveMailTemplateCommand
                          {
                              Id = string.IsNullOrEmpty(lblId.Text) ? (int?)null : Convert.ToInt32(lblId.Text),
                              MailBody = mailBody,
                              MailTemplateName = templateName,
                              MailTemplateType = Convert.ToInt32(ddlMailTemplateTypies.SelectedValue),
                              Title = txtTitle.Text
                          };
            using (var work = UnitOfWork.Begin())
            {
                if (_mailTemplateTask.Save(cmd) != null)
                {
                    work.Commit();
                    ShowMsg("保存成功");
                }
                else
                {
                    ShowMsg("保存失败");
                }
            }
        }
    }
}